home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / AppleScript.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  2.8 KB  |  106 lines  |  [TEXT/Moml]

  1. (* AppleScript.sml *)
  2. (* 1997 Jul 04  e  *)
  3.  
  4. (* This module is a Mac specific implementation of an applescript client
  5.    it provides the capability to
  6.    - compile AppleScript source
  7.    - run the resulting script objects
  8.    - retrieve string results and/or error messages
  9.    - dispose of script objects and results
  10. *)
  11.  
  12. (* references:
  13.    Inside Macintosh: Interapplication Communication, especially Chapter 10
  14.    AppleScript Language Guide English Edition
  15.    -- available at http://applescript.apple.com/support.html
  16. *)
  17.  
  18. (* OSA is Apple's Open Scripting Architecture -- the interface to AppleScript *)
  19.  
  20. type OSAerr = int (* 0 => OK *)
  21. prim_type OSAID_  (* handle to compiled script objects and results *)
  22. type OSAID = { id: OSAID_, ok: bool ref }
  23.  
  24. exception AppleScriptErr of OSAerr * string;
  25.  
  26. local
  27.  
  28.   type OSA_oid = { err : OSAerr,  id : OSAID_ }
  29.   type OSA_str = { err : OSAerr, str : string }
  30.  
  31.   prim_val as_compile_ : string -> OSA_oid = 1 "mac_OSACompile"
  32.   prim_val as_execute_ : OSAID_ -> OSA_oid = 1 "mac_OSAExecute"
  33.   prim_val as_display_ : OSAID_ -> OSA_str = 1 "mac_OSADisplay"
  34.   prim_val as_dispose_ : OSAID_ -> OSAerr  = 1 "mac_OSADispose"
  35.   prim_val as_error_   :   unit -> OSA_str = 1 "mac_OSAScriptError"
  36.  
  37.   fun as_run_script_ xid =
  38.     let
  39.       val { err, id = rid } = as_execute_ xid
  40.       val res = if err = 0
  41.                 then as_display_ rid
  42.                 else as_error_ ()
  43.     in
  44.       as_dispose_ rid;
  45.       res
  46.     end
  47.  
  48.   fun as_run_text_ text = 
  49.     let
  50.       val (ss as { err, id = xid }) = as_compile_ text
  51.     in 
  52.       let val res = if err = 0
  53.                     then as_run_script_ xid
  54.                     else as_error_ ()
  55.       in
  56.         as_dispose_ xid;
  57.         res
  58.       end
  59.     end
  60.  
  61.   fun as_check_str_ { err, str } =
  62.     if err = 0
  63.     then str
  64.     else raise (AppleScriptErr (err,str))
  65.  
  66.   fun as_raise_err _ =
  67.     let val {err,str} = as_error_ ()
  68.     in raise (AppleScriptErr (err,str)) end
  69.  
  70.   fun as_check_err_ err { id, ok } =
  71.     if err = 0
  72.     then ok := false
  73.     else as_raise_err ()
  74.  
  75.   fun as_check_oid_ { err = ror, id = rid } =
  76.     if ror = 0
  77.     then { id = rid, ok = ref true }
  78.     else as_raise_err ()
  79.  
  80.   fun as_check_osaid_ { id = xid, ok } =
  81.     if !ok
  82.     then xid 
  83.     else raise (AppleScriptErr (~1,"Attempt to use disposed OSAID"))
  84.  
  85. in
  86.  
  87. (*  as_compile -- compiles a text script for later execution *)
  88.  
  89. fun as_compile txt = as_check_oid_ (as_compile_ txt);
  90.  
  91. (*  as_dispose -- disposes the compiled script freeing AS memory *)
  92.  
  93. fun as_dispose s   = as_check_err_ (as_dispose_ (as_check_osaid_ s)) s;
  94.  
  95. (*  as_run_script -- runs a precompiled script *)
  96.  
  97. fun as_run_script s = as_check_str_ (as_run_script_ (as_check_osaid_ s));
  98.  
  99. (*  as_run_text -- runs a one shot script, like system() but with a result *)
  100.  
  101. fun as_run_text txt = as_check_str_ (as_run_text_ txt)
  102.  
  103. end;
  104.  
  105. (* end of AppleScript.sml *)
  106.